using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Resources;
using System.Reflection;

using FridayShopping.DataModel;

namespace FridayShopping.UI
{
	/// <summary>
	/// Summary description for ShopPropertiesForm.
	/// </summary>
	public class ShopPropertiesForm : System.Windows.Forms.Form, IItemPropertiesForm
	{
		private System.Windows.Forms.Label lbl_Name;
		private System.Windows.Forms.TextBox edit_Name;
		private System.Windows.Forms.Button btn_Cancel;
		private System.Windows.Forms.ColumnHeader columnHeader_Use;
		private System.Windows.Forms.ColumnHeader columnHeader_Category;
		private System.Windows.Forms.ColumnHeader columnHeader_Key;
		private System.Windows.Forms.ListView listView;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.Button btn_Route;
		private Microsoft.WindowsCE.Forms.InputPanel inputPanel1;
		private System.Windows.Forms.MenuItem menuItem1;

		private Shop shop;
	

		public ShopPropertiesForm(Shop shop)
		{
			this.shop = shop;
			InitializeComponent();
		}

		public ShopPropertiesForm()
		{
			InitializeComponent();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ShopPropertiesForm));
			this.lbl_Name = new System.Windows.Forms.Label();
			this.edit_Name = new System.Windows.Forms.TextBox();
			this.listView = new System.Windows.Forms.ListView();
			this.columnHeader_Use = new System.Windows.Forms.ColumnHeader();
			this.columnHeader_Key = new System.Windows.Forms.ColumnHeader();
			this.columnHeader_Category = new System.Windows.Forms.ColumnHeader();
			this.btn_Cancel = new System.Windows.Forms.Button();
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.btn_Route = new System.Windows.Forms.Button();
			this.inputPanel1 = new Microsoft.WindowsCE.Forms.InputPanel();
			// 
			// lbl_Name
			// 
			this.lbl_Name.Enabled = ((bool)(resources.GetObject("lbl_Name.Enabled")));
			this.lbl_Name.Font = ((System.Drawing.Font)(resources.GetObject("lbl_Name.Font")));
			this.lbl_Name.Location = ((System.Drawing.Point)(resources.GetObject("lbl_Name.Location")));
			this.lbl_Name.Size = ((System.Drawing.Size)(resources.GetObject("lbl_Name.Size")));
			this.lbl_Name.Text = resources.GetString("lbl_Name.Text");
			this.lbl_Name.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("lbl_Name.TextAlign")));
			this.lbl_Name.Visible = ((bool)(resources.GetObject("lbl_Name.Visible")));
			// 
			// edit_Name
			// 
			this.edit_Name.Enabled = ((bool)(resources.GetObject("edit_Name.Enabled")));
			this.edit_Name.Font = ((System.Drawing.Font)(resources.GetObject("edit_Name.Font")));
			this.edit_Name.Location = ((System.Drawing.Point)(resources.GetObject("edit_Name.Location")));
			this.edit_Name.MaxLength = ((int)(resources.GetObject("edit_Name.MaxLength")));
			this.edit_Name.Multiline = ((bool)(resources.GetObject("edit_Name.Multiline")));
			this.edit_Name.PasswordChar = ((char)(resources.GetObject("edit_Name.PasswordChar")));
			this.edit_Name.ScrollBars = ((System.Windows.Forms.ScrollBars)(resources.GetObject("edit_Name.ScrollBars")));
			this.edit_Name.Size = ((System.Drawing.Size)(resources.GetObject("edit_Name.Size")));
			this.edit_Name.Text = resources.GetString("edit_Name.Text");
			this.edit_Name.TextAlign = ((System.Windows.Forms.HorizontalAlignment)(resources.GetObject("edit_Name.TextAlign")));
			this.edit_Name.Visible = ((bool)(resources.GetObject("edit_Name.Visible")));
			this.edit_Name.WordWrap = ((bool)(resources.GetObject("edit_Name.WordWrap")));
			this.edit_Name.GotFocus += new System.EventHandler(this.edit_Name_GotFocus);
			// 
			// listView
			// 
			this.listView.CheckBoxes = true;
			this.listView.Columns.Add(this.columnHeader_Use);
			this.listView.Columns.Add(this.columnHeader_Key);
			this.listView.Columns.Add(this.columnHeader_Category);
			this.listView.Enabled = ((bool)(resources.GetObject("listView.Enabled")));
			this.listView.FullRowSelect = true;
			this.listView.Location = ((System.Drawing.Point)(resources.GetObject("listView.Location")));
			this.listView.Size = ((System.Drawing.Size)(resources.GetObject("listView.Size")));
			this.listView.Text = resources.GetString("listView.Text");
			this.listView.View = System.Windows.Forms.View.Details;
			this.listView.Visible = ((bool)(resources.GetObject("listView.Visible")));
			// 
			// columnHeader_Use
			// 
			this.columnHeader_Use.Text = resources.GetString("columnHeader_Use.Text");
			this.columnHeader_Use.TextAlign = ((System.Windows.Forms.HorizontalAlignment)(resources.GetObject("columnHeader_Use.TextAlign")));
			this.columnHeader_Use.Width = ((int)(resources.GetObject("columnHeader_Use.Width")));
			// 
			// columnHeader_Key
			// 
			this.columnHeader_Key.Text = resources.GetString("columnHeader_Key.Text");
			this.columnHeader_Key.TextAlign = ((System.Windows.Forms.HorizontalAlignment)(resources.GetObject("columnHeader_Key.TextAlign")));
			this.columnHeader_Key.Width = ((int)(resources.GetObject("columnHeader_Key.Width")));
			// 
			// columnHeader_Category
			// 
			this.columnHeader_Category.Text = resources.GetString("columnHeader_Category.Text");
			this.columnHeader_Category.TextAlign = ((System.Windows.Forms.HorizontalAlignment)(resources.GetObject("columnHeader_Category.TextAlign")));
			this.columnHeader_Category.Width = ((int)(resources.GetObject("columnHeader_Category.Width")));
			// 
			// btn_Cancel
			// 
			this.btn_Cancel.Enabled = ((bool)(resources.GetObject("btn_Cancel.Enabled")));
			this.btn_Cancel.Font = ((System.Drawing.Font)(resources.GetObject("btn_Cancel.Font")));
			this.btn_Cancel.Location = ((System.Drawing.Point)(resources.GetObject("btn_Cancel.Location")));
			this.btn_Cancel.Size = ((System.Drawing.Size)(resources.GetObject("btn_Cancel.Size")));
			this.btn_Cancel.Text = resources.GetString("btn_Cancel.Text");
			this.btn_Cancel.Visible = ((bool)(resources.GetObject("btn_Cancel.Visible")));
			this.btn_Cancel.Click += new System.EventHandler(this.btn_Cancel_Click);
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.Add(this.menuItem1);
			// 
			// menuItem1
			// 
			this.menuItem1.Enabled = ((bool)(resources.GetObject("menuItem1.Enabled")));
			this.menuItem1.Text = resources.GetString("menuItem1.Text");
			// 
			// btn_Route
			// 
			this.btn_Route.Enabled = ((bool)(resources.GetObject("btn_Route.Enabled")));
			this.btn_Route.Font = ((System.Drawing.Font)(resources.GetObject("btn_Route.Font")));
			this.btn_Route.Location = ((System.Drawing.Point)(resources.GetObject("btn_Route.Location")));
			this.btn_Route.Size = ((System.Drawing.Size)(resources.GetObject("btn_Route.Size")));
			this.btn_Route.Text = resources.GetString("btn_Route.Text");
			this.btn_Route.Visible = ((bool)(resources.GetObject("btn_Route.Visible")));
			this.btn_Route.Click += new System.EventHandler(this.btn_Route_Click);
			// 
			// ShopPropertiesForm
			// 
			this.ClientSize = ((System.Drawing.Size)(resources.GetObject("$this.ClientSize")));
			this.Controls.Add(this.btn_Route);
			this.Controls.Add(this.btn_Cancel);
			this.Controls.Add(this.listView);
			this.Controls.Add(this.edit_Name);
			this.Controls.Add(this.lbl_Name);
			this.Enabled = ((bool)(resources.GetObject("$this.Enabled")));
			this.Font = ((System.Drawing.Font)(resources.GetObject("$this.Font")));
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Location = ((System.Drawing.Point)(resources.GetObject("$this.Location")));
			this.Menu = this.mainMenu1;
			this.Text = resources.GetString("$this.Text");
			this.Visible = ((bool)(resources.GetObject("$this.Visible")));
			this.Closing += new System.ComponentModel.CancelEventHandler(this.ShopPropertiesForm_Closing);
			this.Load += new System.EventHandler(this.ShopPropertiesForm_Load);

		}
		#endregion

		#region IItemPropertiesForm Members
		public ItemBase GetItem(EditMode mode)
		{
			if (shop == null && mode == EditMode.Add) 
			{
				shop = new Shop();
			}
			FillItemData(shop);

			return shop;
		}

		public void SetDefaultValue(object obj)
		{
		}
		#endregion

		#region Fill/Get the categories of a shop
		private void FillCategories()
		{
			Categories categories = Database.Instance.Categories;

			listView.Items.Clear();

			foreach (Category category in categories.List)
			{
				ListViewItem listViewitem = CreateListViewItem(category);
				if (shop != null) 
				{
					listViewitem.Checked = (shop.Categories.IndexOf(category.Key) >=0);
				}
				listView.Items.Add(listViewitem);
			}
		}

		private ListViewItem CreateListViewItem(Category category)
		{
			ListViewItem i = new ListViewItem();
			i.SubItems.Add(category.Key.ToString());
			i.SubItems.Add(category.Name);
			return i;
		}

		private void FillItemData(Shop shop)
		{
			shop.Name = edit_Name.Text;
			// Remove the unchecked items and add the check ones if they
			// not added yet.
			foreach (ListViewItem listItem in listView.Items) 
			{
				Key key = new Key(Convert.ToInt32(listItem.SubItems[1].Text));

				if (listItem.Checked) 
				{
					if (shop.Categories.IndexOf(key) < 0) 
					{
						shop.Categories.Add(key);
					}
				} 
				else 
				{
					if (shop.Categories.IndexOf(key) >= 0) 
					{
						shop.Categories.Remove(key);
					}
				}
			}
		}
		#endregion

		#region Form Event Handlers

		private void ShopPropertiesForm_Load(object sender, System.EventArgs e)
		{
			if (shop != null) 
			{
				edit_Name.Text = shop.Name;		
			}

			FillCategories();
		}

		private void btn_Cancel_Click(object sender, System.EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
		}

		private void ShopPropertiesForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if (this.DialogResult != DialogResult.Cancel) 
			{
				string name = edit_Name.Text;
				if (name.Trim().Length == 0) 
				{
					MessageBox.Show(LangResources.Instance.Resources.GetString("emptyShopName"), 
						LangResources.Instance.Resources.GetString("applName"));
					e.Cancel = true;
				}
			}		
			inputPanel1.Enabled = false;
		}
	
		private void btn_Route_Click(object sender, System.EventArgs e)
		{
			Shop currentShop = null;
			if (shop == null) 
			{
				currentShop = (Shop)GetItem(EditMode.Add);
			} 
			else 
			{
				currentShop = (Shop)GetItem(EditMode.Modify);
			}

			if (currentShop != null) 
			{
				ShopRoute form = new ShopRoute(currentShop);
				form.ShowDialog();
				// Update the new order of the categories
				shop.Categories = form.CategoryKeys;
			}
		}

		private void edit_Name_GotFocus(object sender, System.EventArgs e)
		{
			inputPanel1.Enabled = true;
		}

		#endregion

	}
}
